package com.drug.mingkang.permission.controller;


import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.permission.service.PerUsersService;
import com.drug.mingkang.utils.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author : 文昌剑
 * @createDate : 2020年6月16日
 */
@RequestMapping(value = "/admin")
@RestController
public class AdminController {

	@Resource
	private PerUsersService perUsersService;


	@Resource
	ResponseMessage responseMessage;

	@Resource
	StringRedisTemplate stringRedisTemplate;

	/**
	 * 根据用户名重置密码
	 * @param userPassword  要重置的密码
	 * @param userName		用户名
	 * @throws IOException
	 */
	@ApiOperation(value = "根据用户名重置密码", notes = "输入用户名和要修改的密码", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping(value = "/resetPassword")
	public ResponseMessage resetPassword(String userPassword, String userName) throws IOException {
		return perUsersService.resetPassword(userPassword,userName);
	}

	/**
	 * 验证用户
	 * @param perUsers
	 * @throws IOException
	 */
	@ApiOperation(value = "验证用户", notes = "验证用户", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping(value = "/verifyUser")
	public ResponseMessage verifyUser(PerUsers perUsers,String captcha) throws IOException {
		//获取验证码
		String code = stringRedisTemplate.opsForValue().get(perUsers.getUserPhone());
		PerUsers perUsers1 = perUsersService.findUsers(perUsers.getUserName());
		if(StringUtils.isEmpty(perUsers1)){
			return ResponseMessage.error("用户名错误！");
		}else if(!perUsers.getUserPhone().equals(perUsers1.getUserPhone())){
			return ResponseMessage.error("用户与当前手机号不匹配！");
		}else if(StringUtils.isEmpty(code) || !captcha.equals(code)){
			return ResponseMessage.error("您所输入的验证码有误！");
		}else{
			return ResponseMessage.success("验证用户成功！");
		}
	}

	/**
	 * 登录验证
	 * @param username
	 *            用户名
	 * @param password
	 *            用户密码
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "登录验证", notes = "登录验证", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping(value = "/getLogin")
	public ResponseMessage getLogin(@RequestParam(value = "username") String username,
									@RequestParam(value = "password") String password, HttpSession session) {
		PerUsers perUsers = perUsersService.findUser(username, password);
		boolean flag = StringUtils.isEmpty(perUsers) ;
		if(flag){
			return ResponseMessage.error("登入失败");
		}
		session.setAttribute("perUsers",perUsers);
		session.setAttribute("loginUserId", perUsers.getUserId());
		stringRedisTemplate.opsForValue().set("loginUser" + perUsers.getUserId(), session.getId());
		return ResponseMessage.success("登入成功",perUsers);
	}

	/**
	 * 注册用户
	 * @param perUsers 实例对象
	 * @param captcha 验证码
	 * @return
	 */
	@ApiOperation(value = "注册用户", notes = "注册用户", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping(value = "/getRegister")
	public ResponseMessage getRegister(PerUsers perUsers,String captcha){
		System.out.println("users.toString() = " + perUsers.toString());
		perUsers.setCreatePerson("注册用户");
		//注册用户都需要经过审核  无效状态
		perUsers.setWhetherValid(2);
		String code = stringRedisTemplate.opsForValue().get(perUsers.getUserPhone());
		System.out.println("code = " + code);
		if(!captcha.equals(code)){
			responseMessage.setStatus(6);
			responseMessage.setMessage("您所输入的验证码有误！");
			return responseMessage;
		}
		return perUsersService.insertSelective(perUsers);
	}

	/**
	 * 显示用户信息
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "显示用户信息", notes = "显示用户信息", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping(value = "/userMessage")
	public ResponseMessage userMessage(HttpSession session) throws IOException {
		PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
		return StringUtils.isEmpty(perUsers) ? ResponseMessage.error("没有登入") : ResponseMessage.success("已登录",perUsers);
	}

	/**
	 * 注销登录
	 * 
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "注销登录", notes = "注销登录", httpMethod = "POST", response = ResponseMessage.class)
	@PostMapping("/exitLogon")
	public ResponseMessage exitLogon(HttpSession session) {
		session.invalidate();
		return ResponseMessage.success("退出成功");
	}
}
